<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style>
        .top {
            float: right;
            position: relative;
            top: -25px;
            background-color: rgb(39, 39, 112);
        }

        .btn-primary {
            margin-left: 10px;
        }

        .price {
            width: 100px;
            height: 34px;
            outline: none;
            border: 1px solid rgb(173, 171, 171);
            border-radius: 4px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">后台管理</h3>
                <a href="creat.html"><button class="btn btn-primary top">创建新的类</button></a>
                <a href="sel.html"><button class="btn btn-primary top">查看租借情况</button></a>
                <a href="sell.html"><button class="btn btn-primary top">查看出售情况</button></a>
            </div>
            <div class="panel-body form-inline">
                <label for="">
                    名称:
                    <input type="text" v-model="goodname" class="form-control">
                </label>
                <label for="">
                    租价:
                    <input type="text" v-model="goodprice" class="form-control">
                </label>
                <label for="">
                    售价:
                    <input type="text" v-model="goodsell" class="form-control">
                </label>
                <label for="">
                    库存:
                    <input type="text" v-model="stock" class="form-control">
                </label>
                <label for="">
                    图片:
                    <input type="text" placeholder="请输入图片存储地址" v-model="img" class="form-control">
                </label>
                <label for="">
                    类别:
                    <input type="text" v-model="object" class="form-control">
                </label>
                <button @click="add" class="btn btn-primary">添加</button>
            </div>
        </div>

        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>名称</th>
                    <th>租价</th>
                    <th>售价</th>
                    <th>库存</th>
                    <th>类别</th>
                    <th>上架时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td> {{item.id}} </td>
                    <td> {{item.goodname}} </td>
                    <td> {{item.goodprice}}元/天 </td>
                    <td> {{item.goodsell}}元</td>
                    <td> {{item.stock}} </td>
                    <td> {{item.object}} </td>
                    <td> {{item.data}} </td>
                    <td> <a href="" @click.prevent="del(item.id)">删除</a>
                        <a href="updata.html"
                            @click="updata(item.id,item.goodname,item.goodprice,item.goodsell,item.stock)">修改</a>
                    </td>
                </tr>
            </tbody>
            <span> 共 {{list.length}} 个商品 </span>
        </table>
    </div>
</body>
<script src="js/vue.js"></script>
<script src="js/vue-resource.min.js"></script>
<script>
    Vue.http.options.emulateJSON = true;

    var vm = new Vue({
        el: "#app",
        data: {
            goodname: '',
            goodprice: '',
            goodsell: '',
            stock: '',
            img: '',
            object: '',
            list: []
        },
        created() {
            this.getAllList();
        },
        methods: {
            getAllList: function () {
                this.$http.get('php/admin_show.php').then(function (result) {
                    this.list = result.body;
                })
            },
            add: function () {
                this.$http.post('php/insertgoods.php', {
                    goodname: this.goodname,
                    goodprice: this.goodprice,
                    goodsell: this.goodsell,
                    stock: this.stock,
                    img: this.img,
                    object: this.object,
                    time: this.time(),
                }, {
                    emulateJSON: true
                }).then(function (result) {
                    if (result.code == 0) {
                        //成功了,添加完成后再调用一下getAllList()
                        alert(result.body.message);
                        // this.getAllList();
                    } else {
                        alert(result.body.message)
                    }
                })
                this.getAllList();
            },
            del: function (id) {
                this.$http.post('php/delete.php', {
                        id: id,
                    }, {
                        emulateJSON: true
                    })
                    .then(function (result) {
                        if (result.body.code == 0) {
                            alert('删除成功!!')
                            this.getAllList()
                        } else {
                            alert('删除失败!!')
                        }
                    })
            },
            time: function () {
                var d = new Date();
                var year = d.getFullYear();
                var month = d.getMonth() + 1;
                var day = d.getDate();
                var str = year + '年' + month + '月' + day + '日';
                return str;
            },
            updata: function (id, goodname, userprice, usersell, stock) {
                localStorage.setItem('id', JSON.stringify(id));
                localStorage.setItem('goodname', JSON.stringify(goodname));
                localStorage.setItem('goodprice', JSON.stringify(goodprice));
                localStorage.setItem('goodsell', JSON.stringify(goodsell));
                localStorage.setItem('stock', JSON.stringify(stock));
            }
        }
    })
</script>

</html>